package com.leetcode.algorithm.y22.m08.w5;

/**
 * 643. 子数组最大平均数 I
 * 
 * https://leetcode.cn/problems/maximum-average-subarray-i/
 * 
 * @author jie.deng
 *
 */
class Question0643Solution01 {

	public double findMaxAverage(int[] nums, int k) {
		int len = nums.length;
		int maxSum = Integer.MIN_VALUE;
		int sum = 0;
		int left = 0;
		int right = 0;
		for (; right < k - 1; right++) {
			sum += nums[right];
		}
		for (; right < len; right++) {
			sum += nums[right];
			maxSum = Math.max(maxSum, sum);
			sum -= nums[left];
			left++;
		}
		return (double) maxSum / k;
	}

}